package org.admin.entity.dto;

import core.validate.Rule;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.admin.entity.BaseDto;

import java.util.HashMap;
import java.util.List;
import java.util.Objects;

@EqualsAndHashCode(callSuper = true)
@Data
public class MenuDto extends BaseDto<MenuDto> {
    protected MenuDto inst(){
        return this;
    }

    public MenuDto(){
        addRule(new Rule<?>[] {
                Rule.Number("appId")
                        .min(1, "应用ID格式错误"),
                Rule.Number("display")
                        .in(new Integer[]{ 0, 1 }, "菜单显示参数错误"),

                Rule.String("action")
                        .max(100, "动作接口长度错误")
                        .regex("^/?[0-9a-zA-A_-]+(/[0-9a-zA-A_-]+)*", "动作接口格式错误"),
                Rule.String("name")
                        .max(16, "菜单名称长度过长"),
                Rule.String("icon")
                        .max(100, "图标地址长度过长"),

                Rule.String("detail").max(200, "菜单描述过长"),

        }).addRule(Rule.<String>NumberGroup(new HashMap<>(){{
            put("id", "菜单ID");
            put("groupId", "功能组ID");
            put("parentId", "上级菜单ID");
        }}, (rule, title) -> {
            rule.min(0, title + "格式错误");
        }).toArray(
                new Rule<?> [0])
        ).addRule(new Rule<?>[]{
                Rule.Number("current").onPreCheck(
                        value -> Objects.requireNonNullElse(value, 1)
                ).min(1, "当前页数不正确"),
                Rule.Number("size").onPreCheck(
                        value -> Objects.requireNonNullElse(value, 20)
                ).min(1, "当前数据条目不正确"),
                Rule.Array("ids").min(1, "未指定删除项"),
        });
        // .integer("无效的页数"),
    }

    private Integer id;
    private String action;
    private String name;
    private Integer appId;
    private Integer parentId;
    private String icon;
    private Integer display;
    private String detail;
    private Integer sort;
    private Integer groupId;
    private Integer current;
    private Integer size;
    private List<Integer> ids;


}